/*****************************************************************************************/
/* ECSE 426 - Microprocessor - Fall 2009
/* Lab Experiment 2 - UART Control
/* UART.C
/* @author: Danny Wu - 260238209 - B.Comp.Eng
/* @author: Han-Sheng Chin - 260209715 - B.Elec.Eng
/* @version: 09.09.18.1304 
/****************************************************************************************/
#include <msp430x14x.h>
#include <stdlib.h>
#include <cross_studio_io.h>
#include <main.h>
#define GRID_SIZE 8
#define NUM_ELEMENTS 0
#define MASK '*'
#define TRUE 1
#define FALSE 0
//#define DEBUG_MODE TRUE


//void main(void){
//  int key;
//  char character;
//    
//  WDTCTL = WDTPW + WDTHOLD;             // Stop watchdog timer
//  initUART(); 
//  _EINT();
//  clearScreen();
//  print("Welcome to Group 08 - Find matching card game\n");
//
//
//  for (;;){
//        
//    //game start
//    //start timer B
//    initKeypad();
//    _BIS_SR(LPM0_bits); 
//    character = getASCIIfromINT(key);
//    putChar(character);
//    
//  }//end for  
//
//
//}//end main

int correct;
int seconds;
int i, j, k, l;
int a, b, c, d;         // The user input elements to compare
int count, shuffle, match;
char temp;
char answers [8][8];    // Array of answers
int reveal [8][8];      // Array that checks whether answers should be
//displayed or not.
int input [3];
int key [2];
int keyNow;
int keyInput;


// Code for memory game
void main(void)
{
 WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer

 gridGen();            // Generates the grid
 randGrid();           // Randomizes the grid

//  initUART();
//  clearScreen();
//  print("Welcome to memory game.");

 // Starts timing code
 seconds = 0;
 TACTL = TASSEL1 + TACLR;   // ACLK for timer
 TACCTL0 = CCIE;           // enable CCRO interrupt
 TACCR0 = 32767;
 // P1DIR |= 0x01;
 TACTL |= 0x10;
 _EINT();
 //End of timing code

 // Displays a grid of 8x8 "*".
 printGrid();

 // Game keeps running if there are still unmatched pairs.
 while (match < (32))
 {
   // Wait for user input of a set of coordinates.
   userInput(1);

   // Specified cell is displayed.
   printGrid();

   // Wait for user to input another set of coordinates.
   userInput(2);

   // Specified cell is displayed.
   printGrid();

   // Check for a match.
   correct = compare(a, b, c, d);

   // If match, letters are left displayed.
   if (correct == 1)
   {
     putString("You found a match!\n");
     match++;
   } // End if
   // Else no match, after 0.5 seconds, both cells go back to "*".
   else
   {
     putString("Not a match! Please try again!\n");
   } // End else

   // Grid is printed again.
   printGrid();

 }
 putString("Game complete!\nYour time is ");
 // putString(seconds);
 putString(" seconds!");
} // End of code for memory game

// Generate the grid in fixed order for the memory game.
void gridGen(void)
{
 int currentChar = 0;
 for (i = 0; i < 4; i++)
 {
   for (j = 0; j < 8; j++)
   {
     answers [i+i][j] = 'A' + currentChar;
     reveal [i+i][j] = 0;
     answers [i+i+1][j] = answers [i+i][j];
     reveal [i+i+1][j] = 0;
     currentChar++;
   } // End for j
 } // End for i
} // End of gridGen

// Randomize the grid values
void randGrid(void)
{
 shuffle = 32;
 count = 0;
 while (count < shuffle)
 {
   i = rand() % GRID_SIZE;           // Random row for element 1
   j = rand() % GRID_SIZE;           // Random column for element 1
   k = rand() % GRID_SIZE;           // Random row for element 2
   l = rand() % GRID_SIZE;           // Random column for element 2
   temp = answers [i][j];            // Stores original value of
element 1 in temp
   answers [i][j] = answers [k][l];  // Puts value of element 2 into element 1
   answers [k][l] = temp;            // Puts value of temp into element 2
   count++;
 } // End while
} // End of randGrid

// Prints the grid.
void printGrid(void)
{
 for (i = 0; i < 8; i++)
 {
   for (j = 0; j < 8; j++)
   {
     if (reveal[i][j] == 1)
       putChar(answers [i][j]);
     else if (reveal[i][j] == 0)
       putChar('*');
     //putChar(' ');
   } // End for j
   putChar('\n');
 } // End for i
} // End of printGrid


// Get user input
void userInput(int inputNum)
{
 while (keyNow != 11)  // While key being pressed is not #
 {
   if (keyInput)       // If key is being pressed
   {
     if (inputNum == 1)   // If its the first input
     {
       a = b;
       b = keyNow;
       reveal[a][b] = 1;
     } // end if
     else if (inputNum == 2)  // If its the second input
     {
       c = d;
       d = keyNow;
       reveal[c][d] = 1;
     } // end else if
   } // end if
 } // end while
} // End of userInput



// Compare the two elements selected and see if the value is correct
int compare(int a, int b, int c, int d)
{
 if (answers [a][b] == answers [c][d])
 {
   correct = 1;
   match++;
 }
 else
   correct = 0;
   reveal[a][b] = 0;   // The reveal array elements for both cells
                       //are set back to 0
   reveal[c][d] = 0;
} // End of compare


// Timer A0 interrupt service routine
void TimerA_ISR(void) __interrupt[TIMERA0_VECTOR]
{
 seconds++;
 TACTL = TASSEL0 + TACLR;   // ACLK for timer
 CCTL0 = CCIE;           // enable CCRO interrupt
 CCR0 = 32767;
 TACTL |= 0x10;
 CCR0 += 32767;
} // End of A0 ISR

//this method converts the int into char
char getASCIIfromINT(int val) {
  char chara;
  switch(val){
      case 0:   
        chara = '0'; break;
      case 1:   
        chara = '1';break;
      case 2:  
        chara = '2'; break;
      case 3:  
        chara = '3'; break;
      case 4:  
        chara = '4'; break;
      case 5:   
        chara = '5'; break;
      case 6:  
        chara = '6'; break;
      case 7:  
        chara = '7'; break;
      case 8:  
        chara = '8'; break;
      case 9: 
        chara = '9';break;
      case 10:  
        chara = '*'; break;
      case 11:   
        chara = '#'; break;
      default:   //default value
        chara = '!';break;
      }//end switch    
    return chara;
}//end 
